package com.sky.service.user.impl;

import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.mapper.user.AddressBookServiceMapper;
import com.sky.pojo.AddressBook;
import com.sky.result.Result;
import com.sky.service.user.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class AddressBookServiceImpl implements AddressBookService {
    @Autowired
    private AddressBookServiceMapper addressBookServiceMapper;

    @Override
    public void insertAddressBook(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBook.setIsDefault(StatusConstant.DISABLE);
        addressBookServiceMapper.insertAddressBook(addressBook);
    }
    @Override
    public List<AddressBook> queryAddressBookList() {
        Long userId = BaseContext.getCurrentId();
        return addressBookServiceMapper.queryAddressBookList(userId);
    }

    @Override
    public AddressBook queryAcquiesceAddressBook() {
        Long userId = BaseContext.getCurrentId();
        Integer isDefault=StatusConstant.ENABLE;
        List<AddressBook> list=addressBookServiceMapper.queryAcquiesceAddressBook(userId,isDefault);
        if (list != null && list.size() == 1) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public AddressBook queryAddressBookId(Long id) {
        AddressBook addressBook= addressBookServiceMapper.queryAddressBookId(id);
        log.info("地址{}",addressBook);
        return addressBook;
    }

    @Override
    public void updateAddressBook(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBook.setIsDefault(StatusConstant.DISABLE);
        addressBookServiceMapper.updateAddressBook(addressBook);
    }

    @Override
    public void deleteAddressBook(Long id) {
        addressBookServiceMapper.deleteAddressBook(id);
    }

    @Override
    public void setAcquiesceAddressBook(Long id) {
        //将当前的所有地址isDefault设置为0
        Long userId = BaseContext.getCurrentId();
        Integer notDefault=StatusConstant.DISABLE;
        addressBookServiceMapper.CleanAcquiesceAddressBook(userId,notDefault);
        //设置默认地址
        Integer isDefault=StatusConstant.ENABLE;
        addressBookServiceMapper.setAcquiesceAddressBook(id,isDefault);
    }
}
